#include "seqiot.h"
#include "sqlite.h"
#include "sysinfo.h"

int main(int argc, char *argv[]) {
    int rc;
    sqlite3 *db = NULL;

    log_init();

    if (argc > 1) {
        db = sqlite_open(argv[1]);
    } else {
        char buf[255];
        rc = get_conf_file(buf, sizeof(buf));
        if (rc >= 0) {
            db = sqlite_open(buf);
        }
    }

    if (NULL == db) {
        return EXIT_FAILURE;
    }

    vector_t info_list = vector_initializer;
    read_stmt_sys_infos(db, &info_list);

    for (int counter = 0; running; ++counter) {
        if (counter % 30 != 0) {
            sqlite3_sleep(1000);
            continue;
        }
        for (int i = 0; i < vector_size(&info_list); ++i) {
            sys_info_t *info = vector_get(&info_list, i);
            double value;
            rc = sys_info_exec_double(info->cmd, &value);
            if (rc >= 0) {
                log_debug("id=%s, value=%f", info->id, value);
            } else {
                log_error("id=%s, cmd=[%s]", info->id, info->cmd);
            }
        }
    }

    vector_foreach(&info_list, (vector_foreach_callback) sys_info_free);
    vector_clear(&info_list);

    log_fini();

    return EXIT_SUCCESS;
}
